package com.算法.软件设计与开发.试题编号_2_1_4_信息管理系统关键算法;


public class 任务三_信息管理功能关键算法 {
    /**
     * 创建 5 个学生对象，并赋给一个学生数组，每个学生有以下属性：
     * 学号、姓名、年龄，请按顺序实现以下任务：
     *
     * 子任务 1：将学生按学号排序输出。
     * 子任务 2：给所有学生年龄加 1。
     * 子任务 3：在实现子任务 2 的基础上，统计大于 20 岁的学生人数。
     * @param args
     */
    public static void main(String[] args) {
        Student1[] s1 = new Student1[5];
        s1[0] = new Student1(1,"张三",20);
        s1[1] = new Student1(3,"李四",18);
        s1[2] = new Student1(5,"王五",19);
        s1[3] = new Student1(2,"赵六",21);
        s1[4] = new Student1(4,"陈七",19);
        stu(s1);

    }

    public static void stu(Student1[] s1){

        //子任务 1：将学生按学号排序输出。
        for (int i = 0; i < s1.length; i++) {
            for (int j = i+1; j < s1.length; j++) {
                if(s1[i].stu_number >= s1[j].stu_number){
                    swap(s1,j,i);
                }
            }
            //查看按学号排序的学生
            System.out.println(s1[i].stu_number + "_" + s1[i].stu_name + "_" + s1[i].stu_age);
        }
//        for (int i = 0; i < s1.length; i++) {
//            System.out.println(s1[i].stu_number + "_" + s1[i].stu_name + "_" + s1[i].stu_age);
//        }

        //子任务 2：给所有学生年龄加 1。
        int count = 0;
        for (int i = 0; i < s1.length; i++) {
            s1[i].setStu_age(s1[i].stu_age+1);
            //子任务 3：在实现子任务 2 的基础上，统计大于 20 岁的学生人数。
            if(s1[i].stu_age>20){
                count++;
                System.out.println(s1[i].stu_name);
            }
        }

        System.out.println("大于 20 岁的学生人数:"+count);

    }
    public static void swap(Student1[]nums,int j,int i){
        Student1 temp=nums[j];
        nums[j]=nums[i];
        nums[i]=temp;
    }
}
class Student1{
    int stu_number; //学号
    String stu_name; //姓名
    int stu_age; //年龄

    public Student1() {
    }

    public Student1(int stu_number, String stu_name, int stu_age) {
        this.stu_number = stu_number;
        this.stu_name = stu_name;
        this.stu_age = stu_age;
    }

    @Override
    public String toString() {
        return "Student1{" +
                "stu_number='" + stu_number + '\'' +
                ", stu_name='" + stu_name + '\'' +
                ", stu_age=" + stu_age +
                '}';
    }

    public int getStu_number() {
        return stu_number;
    }

    public void setStu_number(int stu_number) {
        this.stu_number = stu_number;
    }

    public String getStu_name() {
        return stu_name;
    }

    public void setStu_name(String stu_name) {
        this.stu_name = stu_name;
    }

    public int getStu_age() {
        return stu_age;
    }

    public void setStu_age(int stu_age) {
        this.stu_age = stu_age;
    }
}